﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using UnityEngine;

namespace DashFire
{
  /// <summary>
  /// AppChanel | AppMaster | ResBase
  /// 16        | 32        | 8
  /// </summary>
  public class VersionNum
  {
    public static string[] C_SplitInterval = new string[] { "." };
    public static int C_VersionSlot = 3;

    public bool IsValid;
    public int AppChannel;
    public int AppMaster;
    public int ResBase;

    public VersionNum(string version)
    {
      try {
        version = version.Trim();
        string[] splitRet = version.Split(C_SplitInterval, StringSplitOptions.RemoveEmptyEntries);
        if (splitRet != null && splitRet.Length == C_VersionSlot) {
          AppChannel = Convert.ToInt32(splitRet[0]);
          AppMaster = Convert.ToInt32(splitRet[1]);
          ResBase = Convert.ToInt32(splitRet[2]);
          IsValid = true;
        } else {
          AppChannel = 0;
          AppMaster = 0;
          ResBase = 0;
          IsValid = false;
          ResLoadHelper.Log("VersionInstance parse error");
        }
      } catch (System.Exception ex) {
        ResLoadHelper.Log("VersionInstance parse error ex:" + ex);
        IsValid = false;
      }
    }
    public ulong GetAppVersionForceValue()
    {
      return (ulong)(((ulong)AppChannel & 0xFFFF) << (40)) + (ulong)(((ulong)AppMaster & 0xFFFFFFFF) << (8));
    }
    public ulong GetResVersionValue()
    {
      return (ulong)(((ulong)ResBase & 0xFF) << 0);
    }
    public ulong GetVersionValue()
    {
      return (ulong)(((ulong)AppChannel & 0xFFFF) << (40))
        + (ulong)(((ulong)AppMaster & 0xFFFFFFFF) << (8))
        + (ulong)(((ulong)ResBase & 0xFF) << (0));
    }
    public string GetAppVersionStr()
    {
      return string.Format("{0}.{1}",
        AppChannel,
        AppMaster);
    }
    public string GetAppVersionForceStr()
    {
      return string.Format("{0}",
        AppMaster);
    }
    public string GeResVersionStr()
    {
      return string.Format("{0}",
        ResBase);
    }
    public string GetVersionStr()
    {
      return string.Format("{0}.{1}.{2}",
        AppChannel,
        AppMaster,
        ResBase);
    }
  }

  public class VersionInfo
  {
    public string IniFile = string.Empty;
    public VersionNum Version;
    public string Platform;
    public string GroupName;
    public string ResServerURL;
    public string ForceDownloadURL;

    public bool IsResVersionConfigCached;
    public bool IsAssetDBConfigCached;
    public bool IsResCacheConfigCached;
    public bool IsResInnerConfigCached;

    public void Load(string iniFile)
    {
      IniFile = iniFile;
      ConfigFile m_IniFile = new ConfigFile(IniFile);
      Version = new VersionNum(m_IniFile.GetSetting("AppSetting", "Version"));
      Platform = m_IniFile.GetSetting("AppSetting", "Platform");
      GroupName = m_IniFile.GetSetting("AppSetting", "GroupName");
      ResServerURL = m_IniFile.GetSetting("AppSetting", "ResServerURL");
      ForceDownloadURL = m_IniFile.GetSetting("AppSetting", "ForceDownloadURL");

      IsResVersionConfigCached = Convert.ToBoolean(m_IniFile.GetSetting("Runtime", "IsResVersionConfigCached"));
      IsAssetDBConfigCached = Convert.ToBoolean(m_IniFile.GetSetting("Runtime", "IsAssetDBConfigCached"));
      IsResCacheConfigCached = Convert.ToBoolean(m_IniFile.GetSetting("Runtime", "IsResCacheConfigCached"));
      IsResInnerConfigCached = Convert.ToBoolean(m_IniFile.GetSetting("Runtime", "IsResSheetConfigCached"));
    }
    public void Load(string iniFile, byte [] buffer)
    {
      IniFile = iniFile;
      ConfigFile m_IniFile = new ConfigFile(IniFile, buffer);
      Version = new VersionNum(m_IniFile.GetSetting("AppSetting", "Version"));
      Platform = m_IniFile.GetSetting("AppSetting", "Platform");
      GroupName = m_IniFile.GetSetting("AppSetting", "GroupName");
      ResServerURL = m_IniFile.GetSetting("AppSetting", "ResServerURL");
      ForceDownloadURL = m_IniFile.GetSetting("AppSetting", "ForceDownloadURL");

      IsResVersionConfigCached = Convert.ToBoolean(m_IniFile.GetSetting("Runtime", "IsResVersionConfigCached"));
      IsAssetDBConfigCached = Convert.ToBoolean(m_IniFile.GetSetting("Runtime", "IsAssetDBConfigCached"));
      IsResCacheConfigCached = Convert.ToBoolean(m_IniFile.GetSetting("Runtime", "IsResCacheConfigCached"));
      IsResInnerConfigCached = Convert.ToBoolean(m_IniFile.GetSetting("Runtime", "IsResSheetConfigCached"));
    }
    public void Save()
    {
      ConfigFile m_IniFile = new ConfigFile(IniFile);
      m_IniFile.AddSetting("AppSetting", "Version", Version.GetVersionStr());
      m_IniFile.AddSetting("AppSetting", "Platform", Platform);
      m_IniFile.AddSetting("AppSetting", "GroupName", GroupName);
      m_IniFile.AddSetting("AppSetting", "ResServerURL", ResServerURL);
      m_IniFile.AddSetting("AppSetting", "ForceDownloadURL", ForceDownloadURL);

      m_IniFile.AddSetting("Runtime", "IsResVersionConfigCached", IsResVersionConfigCached.ToString());
      m_IniFile.AddSetting("Runtime", "IsAssetDBConfigCached", IsAssetDBConfigCached.ToString());
      m_IniFile.AddSetting("Runtime", "IsResCacheConfigCached", IsResCacheConfigCached.ToString());
      m_IniFile.AddSetting("Runtime", "IsResSheetConfigCached", IsResInnerConfigCached.ToString());
      m_IniFile.SaveSettings(IniFile);
    }
  }
}

